package gov.dhs.cis.soa.readers;

import gov.dhs.cis.soa.SOAData;
import gov.dhs.cis.soa.Utils;
import gov.dhs.cis.soa.data.Agent;

import java.util.ArrayList;

import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;


public class AgentsLayerReader extends ExcelReader {
	static final Logger loger = Logger.getLogger(AgentsLayerReader.class);

	private final String AGENT_FILE_NAME = "USCIS Architectural Components - Agent Layer.xls";
	private String SHEET_NAME = "Sheet1";

	private String REL_COL_NUM = "B";
	private String AGENT_COL_NUM = "C";
	private String DESC_COL_NUM = "D";
	private String ST_COL_NUM = "E";
	private String DATA_INIT_COL_NUM = "F";
	private String DATA_RESP_COL_NUM = "G";
	private String EXT_APP_COL_NUM = "H";
	private String EXT_COMP_COL_NUM = "I";
	private String PALCE_COL_NUM = "J";
	private String OWNER_COL_NUM = "K";
	private String EXIST_COL_NUM = "L";
	private String DA_COL_NUM = "M";
	private String MEP_COL_NUM = "N";
	private String SEC_ASP_COL_NUM = "O";
	
	private int MAX_SERVS = 50;
	
	public SOAData extractAgents(SOAData soaData) {
		loger.debug("Extracting Agents layer Sheet...");
		HSSFWorkbook w = openFile(AGENT_FILE_NAME);
		HSSFSheet s1 = w.getSheet(SHEET_NAME);
		int rows = s1.getLastRowNum();
		loger.debug("S1 rows =" + rows);

		ArrayList<Agent> agents = new ArrayList<Agent>();
		soaData.agentList = agents;
		for (int n = 2; n < MAX_SERVS; n++) {
			HSSFRow row = s1.getRow(n);
			if(row == null)
				continue;
			String data = getCellStringValue(w, row, AGENT_COL_NUM);
			if(Utils.isEmpty(data)) {
				loger.warn("Agent Name is Null/Empty at row : " + n + ", ignoring this row.");
				continue;
			}
			Agent a = new Agent();
			a.id = Utils.getNextId();
			a.name = getCellStringValue(w, row, AGENT_COL_NUM);
			
			a.descText = getCellStringValue(w, row, DESC_COL_NUM);
			a.release = Utils.convert2Release(getCellStringValue(w, row, REL_COL_NUM));
			a.stereoType = getCellStringValue(w, row, ST_COL_NUM);
			a.dataInit = getCellStringValue(w, row, DATA_INIT_COL_NUM);
			a.dataResp = getCellStringValue(w, row, DATA_RESP_COL_NUM);
			a.connExtApp = getCellStringValue(w, row, EXT_APP_COL_NUM);
			a.connExtComp = getCellStringValue(w, row, EXT_COMP_COL_NUM);
			a.placement = getCellStringValue(w, row, PALCE_COL_NUM);
			a.owner = getCellStringValue(w, row, OWNER_COL_NUM);
			a.existence = getCellStringValue(w, row, EXIST_COL_NUM);
			a.designAspects = getCellStringValue(w, row, DA_COL_NUM);
			a.mep = getCellStringValue(w, row, MEP_COL_NUM);
			a.securityAspects = getCellStringValue(w, row, SEC_ASP_COL_NUM);

			a.createDescription();
			agents.add(a);
		}
		closeAll(w);
		return soaData;
	}
}
